"""
These are functions used by the river code.

Parker MacCready
"""

def get_ldir():
    """
    This returns the dictionary Ldir
    """
    import os
    import sys
    alp = os.path.abspath('../../../alpha')
    if alp not in sys.path:
        sys.path.append(alp) 
    import Lfun
    reload(Lfun)
    Ldir = Lfun.Lstart(alp)
    return Ldir
    
def fix_units(qt, flow_units):
    # fix units
    if flow_units == 'kcfs':
        qt = qt*28.3168466
    elif flow_units == 'cubic feet per second' or flow_units == 'ft3/s':
        qt = qt*0.0283168466
    return qt
    
def get_nws_data(id):
    """
    This gets NWS data.    
    """
    import urllib2
    import xml.etree.ElementTree as ET
    import pandas as pd
    from pandas import Series, DataFrame 
    import Rfun
    reload(Rfun)
      
    # default values
    Q = []
    T = []
    qt = Series(Q, index=T)
    got_nws_data = False
    memo = 'no message'
         
    url_str = ('http://www.nwrfc.noaa.gov/xml/xml.cgi?id=' + id
    + '&pe=HG&dtype=b&numdays=10')
    try:
        file = urllib2.urlopen(url_str, timeout = 10)
        tree = ET.parse(file)
        root = tree.getroot()
    except:
        memo = 'problem downloading XML'
    
    try:
        flow_units = ''
        flag = True
        # NOTE: you find this tag by looking at any instance of e0.tag
        aa = '{http://www.nwrfc.noaa.gov/xml/schemas/2004/03/hydromet_data}'
        for e0 in root.findall(".//"):
            if e0.tag == aa+'observedData' or e0.tag == aa+'forecastData':
                for e in e0:
                    if e.tag == aa+'observedValue' or e.tag == aa+'forecastValue':
                        for ee in e:
                            if ee.tag == aa+'discharge':
                                Q.append(float(ee.text))
                                if flag:
                                    flow_units = ee.get('units')
                                    flag = False
                            if ee.tag == aa+'dataDateTime':
                                T.append(pd.to_datetime(ee.text))
        qt = Series(Q, index=T)
        qt = Rfun.fix_units(qt, flow_units)
        got_nws_data = True
        memo = 'success'
    except:
        memo = 'problem parsing data from XML'
        
    return qt, got_nws_data, memo
    
def get_usgs_data(id):
    """
    This gets USGS data.
    """
    import urllib2
    import xml.etree.ElementTree as ET
    import pandas as pd
    from pandas import Series, DataFrame 
    import Rfun
    reload(Rfun)
      
    # default values
    Q = []
    T = []
    qt = Series(Q, index=T)
    got_usgs_data = False
    memo = 'no message'
    
    url_str = ('http://waterservices.usgs.gov/nwis/iv/' +
    '?format=waterml,1.1&sites=' + id + '&period=P6D&parameterCd=00060')
    try:
        file = urllib2.urlopen(url_str, timeout = 10)
        tree = ET.parse(file)
        root = tree.getroot()
    except:
        memo = 'problem downloading XML'
                                          
    try:
        flow_units = ''
        flag = True
        aa = '{http://www.cuahsi.org/waterML/1.1/}'
        for e0 in root.findall(".//"):
            if e0.tag == aa+'value':
                Q.append(float(e0.text))
                T.append(pd.to_datetime(e0.get('dateTime')))
            if e0.tag == aa+'unitCode' and flag:
                flow_units = e0.text
                flag = False
        qt = Series(Q, index=T)
        qt = Rfun.fix_units(qt, flow_units)
        got_usgs_data = True
        memo = 'success'
    except:
        memo = 'problem parsing data from XML'
    
    return qt, got_usgs_data, memo

def get_river_code_nws(river_name):
    """
    This goes from a river name to an NWS number.
    """
    
    import pandas as pd

    # get the table of names, gages, and scaling factors from Mohamedali
    inframe = pd.read_csv('./Files_USGS/USGS_good_plus.csv', index_col='Name')

    try:
        has_nws_forecast = inframe.ix[river_name, 'NWS Forecast'] # YES or NO
        this_code_nws = inframe.ix[river_name, 'NWS ID']
        this_code_usgs = str(inframe.ix[river_name, 'Station Number']) 
    except:
        has_nws_forecast = 'NO'
        this_code_nws = 'MISSING'
        this_code_usgs = 'MISSING'
   
    return this_code_nws, has_nws_forecast, this_code_usgs
    
def get_river_code_ecology(river_name):
    """
    This goes from a river name
    to a USGS number and a scale factor, based on the
    spreadsheet by Mohamedali et al. at Ecology.
    """
        
    import pandas as pd

    # get the table of names, gages, and scaling factors from Mohamedali
    inframe = pd.read_csv('./Files_Ecology/Ecology_Scale_Factors.csv')
    name = inframe['Watershed Name'].values
    gage = inframe['Scale Gage'].values
    fac = inframe['Scale Factor'].values

    # pull the station code out
    # issue: most are USGS numbers, but some are 'Hybrid' or like '08HA070' (Canadian)
    # but all are still string items in a list
    gg = []
    for g in gage: # find the item in parentheses
        i1 = g.find('(')
        i2 = g.find(')')
        gg.append(g[i1+1:i2])

    # simplify the names   
    rn = []
    for n in name:
        n = n.strip()
        i1 = n.find(' ')
        if i1 != -1:
            n = n[:i1]
        i2 = n.find('_')
        if i2 != -1:
            n = n[:i2]
        rn.append(n.lower())
        
    # add these to the frame
    inframe['Code'] = gg

    # and index by River Name
    if2 = inframe.set_index([rn])
    
    
    try:
        this_code = if2.ix[river_name, 'Code']
        this_scale = if2.ix[river_name, 'Scale Factor']
    except:
        this_code = 'MISSING'
        this_scale = 1
    
    return this_code, this_scale




